package core;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import util.*;

public class LoginGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	NetClient netClient;
	private Client client;
	MainGUI clientFrame;

	private JPanel contentPane;
	private JLabel lblPort;
	private JLabel lblHost;
	private JLabel lblLogin;
	private JTextField loginField;
	private JTextField portField;
	private JTextField hostField;

	public LoginGUI() {
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 300, 200);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[] {150, 150};
		gbl_contentPane.rowHeights = new int[] {0, 0, 0, 0, 30, 30};
		gbl_contentPane.columnWeights = new double[]{0.0, 1.0};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
		contentPane.setLayout(gbl_contentPane);
		
		lblHost = new JLabel("Host");
		GridBagConstraints gbc_lblPort = new GridBagConstraints();
		gbc_lblPort.insets = new Insets(0, 0, 5, 5);
		gbc_lblPort.anchor = GridBagConstraints.WEST;
		gbc_lblPort.gridx = 0;
		gbc_lblPort.gridy = 1;
		contentPane.add(lblHost, gbc_lblPort);
		
		hostField = new JTextField("localhost");
		GridBagConstraints gbc_hostField = new GridBagConstraints();
		gbc_hostField.insets = new Insets(0, 0, 5, 0);
		gbc_hostField.fill = GridBagConstraints.HORIZONTAL;
		gbc_hostField.gridx = 1;
		gbc_hostField.gridy = 1;
		contentPane.add(hostField, gbc_hostField);
		hostField.setColumns(10);

		hostField.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode()==KeyEvent.VK_ENTER){
					connect();
				}
			}
		});
		
		lblPort = new JLabel("Port");
		GridBagConstraints gbc_lblPort_1 = new GridBagConstraints();
		gbc_lblPort_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblPort_1.anchor = GridBagConstraints.WEST;
		gbc_lblPort_1.gridx = 0;
		gbc_lblPort_1.gridy = 2;
		contentPane.add(lblPort, gbc_lblPort_1);
		
		portField = new JTextField("5555");
		GridBagConstraints gbc_portField = new GridBagConstraints();
		gbc_portField.insets = new Insets(0, 0, 5, 0);
		gbc_portField.fill = GridBagConstraints.HORIZONTAL;
		gbc_portField.gridx = 1;
		gbc_portField.gridy = 2;
		contentPane.add(portField, gbc_portField);
		portField.setColumns(10);
				
		lblLogin = new JLabel("Username:");
		GridBagConstraints gbc_loginLbl = new GridBagConstraints();
		gbc_loginLbl.fill = GridBagConstraints.BOTH;
		gbc_loginLbl.insets = new Insets(0, 0, 5, 5);
		gbc_loginLbl.gridx = 0;
		gbc_loginLbl.gridy = 4;
		contentPane.add(lblLogin, gbc_loginLbl);
		
		loginField = new JTextField("M8nBlade");
		GridBagConstraints gbc_loginField = new GridBagConstraints();
		gbc_loginField.anchor = GridBagConstraints.NORTH;
		gbc_loginField.fill = GridBagConstraints.HORIZONTAL;
		gbc_loginField.insets = new Insets(0, 0, 5, 0);
		gbc_loginField.gridx = 1;
		gbc_loginField.gridy = 4;
		contentPane.add(loginField, gbc_loginField);
		loginField.setColumns(10);
		
		JButton loginBtn = new JButton("Connect!");
		loginBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				connect();
			}
		});
		GridBagConstraints gbc_loginBtn = new GridBagConstraints();
		gbc_loginBtn.gridwidth = 2;
		gbc_loginBtn.fill = GridBagConstraints.BOTH;
		gbc_loginBtn.gridx = 0;
		gbc_loginBtn.gridy = 5;
		contentPane.add(loginBtn, gbc_loginBtn);
		
		setLocationRelativeTo(null);
		setResizable(false);
		pack();
	}

	public void connect(){
		String host = hostField.getText();
		int port;
		try{
			port = Integer.parseInt(portField.getText());
		} catch (NumberFormatException numExc){
			portField.setText("Port incorrect");
			return;
		}
		if(port < 0 || port > 65535 ) {
			portField.setText("Port incorrect");
			return;
		}
		
		String username = loginField.getText();
		if (username.equals("") ){
			loginField.setText("Username empty");
			return;
		}
		
		client = new Client(username);
		
		netClient = new NetClient(client);
		
		if (!netClient.connect(host, port)){
			hostField.setText("Server or Port incorrect");
			return;
		}
		
		netClient.sendMessage("LOGIN;" + username);
		String loginMessage = netClient.receiveMessage();
		
		if( !loginMessage.equals("LOGINSUCCESS") ) {
			loginField.setText("Username incorrect");
			return;
		} 
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					clientFrame = new MainGUI(client, netClient);
					clientFrame.setVisible(true);
					clientFrame.setAlwaysOnTop(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		netClient.start();
		setVisible(false);
	}
}
